package com.sinosoft.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.GenericDaoHibernate;
import com.sinosoft.user.User;
import com.sinosoft.util.CheckEmpty;

@Repository
public class ReportUserDaoHibernate extends GenericDaoHibernate<User, Long>
		implements ReportUserDao {

	public ReportUserDaoHibernate() {
		super(User.class);
	}

	/***
	 * 用户统计
	 */
	public List<ReportView> searchUser(String hsql, String[] colums) {
		List<ReportView> resultlist = null;
		ReportView vo = null;
		Session session = null;
		Map<String, String> map = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			Query query = session.createQuery(hsql);
			List list = query.list();

			if (null != list && list.size() > 0) {
				resultlist = new ArrayList<ReportView>();
				if (colums.length > 1) {
					Iterator iterator = list.iterator();
					Object[] obj = null;
					while (iterator.hasNext()) {
						int i = 0;
						map = new HashMap<String, String>();
						obj = (Object[]) iterator.next();
						for (int j = 0; j < obj.length; j++) {
							if (obj[j] != null) {
								map.put(colums[j], obj[j].toString());
							} else {
								map.put(colums[j], "");
							}

						}
						vo = new ReportView();
						vo.setTerritory(map.get("territory"));
						vo.setCompany(map.get("company"));
						vo.setGrade(map.get("grade"));
						vo.setSatellte(map.get("satelliteid"));
						vo.setUsername(map.get("username"));
						vo.setCreateuser(map.get("createuser"));
						vo.setProductlevel(map.get("productlevel"));
						vo.setSensor(map.get("sensorid"));
						vo.setReceivestation(map.get("receivestationid"));
						vo.setOrderstate(map.get("orderstate"));
						vo.setCollectStatus(map.get("taskStatus"));
						vo.setNum(Integer.parseInt(map.get("num")));
						resultlist.add(vo);
					}
				} else {
					if (null != list.get(0)) {
						resultlist = new ArrayList<ReportView>();
						vo = new ReportView();
						vo.setNum(Integer.parseInt(list.get(0).toString()));
						resultlist.add(vo);
					}

				}

			}
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			session.close();
		}
		return resultlist;
	}

	/**
	 * 用户数量统计
	 */
	public List<ReportView> searchUser(String hql) {
		if (CheckEmpty.isEmpty(hql)) {
			return null;
		}
		List list = new ArrayList();
		ReportView vo = null;
		Session session = null;

		try {
			session = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			Query query = session.createQuery(hql);
			List datelist = query.list();
			if (null != datelist && datelist.size() > 0) {
				Iterator iterator = datelist.iterator();
				Object[] obj = null;
				while (iterator.hasNext()) {
					obj = (Object[]) iterator.next();
					vo = new ReportView();
					if (null == obj[0]) {
						vo.setValue("");
					} else {
						String value = obj[0].toString();
						vo.setValue(value);
					}
					if (obj[1] != null) {
						String num = obj[1].toString();
						vo.setNum(Integer.parseInt(num));
					}
					list.add(vo);
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	public List searchvisit(String sqls) {
		if (CheckEmpty.isEmpty(sqls)) {
			return null;
		}
		ReportView vo = null;
		List list = new ArrayList();
		try {
			Session session = getHibernateTemplate().getSessionFactory()
					.openSession();
			SQLQuery query = session.createSQLQuery(sqls);
			List datelist = query.list();
			if (null != datelist && datelist.size() > 0) {
				Iterator iterator = datelist.iterator();
				Object[] obj = null;
				while (iterator.hasNext()) {
					obj = (Object[]) iterator.next();
					vo = new ReportView();
					if (null == obj[0]) {
						vo.setValue("");
					} else {
						String value = obj[0].toString();
						vo.setValue(value);
					}
					if (obj[1] != null) {
						String num = obj[1].toString();
						vo.setNum(Integer.parseInt(num));
					}
					list.add(vo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}
}
